﻿using DHQGHN.Common;
using DHQGHN.DAL.BO.DHQG;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using DHQGHN.PM2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DHQGHN.PM2.Controllers
{
    public class NhomDonViController : BaseController
    {
        public ActionResult Index(NhomDonViModel a = null, int pageIndex = 0, string sort = "Id", string sortDir = "ASC", string curentID = "")
        {
            ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
            //ViewBag.ActiveMenus[13] = "active";
            //ViewBag.ActiveMenus[249] = "active";
            string strInputMenuUrl = "/NhomDonVi/Index";
            List<DHQGHN.PM2.Models.MenuModel> listMenu = Session["MenuList"] as List<DHQGHN.PM2.Models.MenuModel>;
            if (listMenu.Count > 0)
            {
                for (int i = 0; i < listMenu.Count; i++)
                {
                    if (listMenu[i].MenuUrl.ToUpper().Equals(strInputMenuUrl.ToUpper()))
                    {
                        ViewBag.ActiveMenus[listMenu[i].ParentMenuId] = "active";
                        ViewBag.ActiveMenus[listMenu[i].MenuId] = "active";
                        ViewBag.CurrentMenuName = listMenu[i].MenuName;
                        ViewBag.Title = listMenu[i].MenuName;
                    }
                }
            }


            //a.DELETED = 2;
            //a.Levels = 0;

            //truong hop load form --> tim tat ca
            a.IS_CO_BAN = 2;
            var model = Search(a, pageIndex, curentID, false);
            return View(model);
        }

        [HttpPost]
        public ActionResult CreatePopupUpdate(string id)
        {
            NhomDonViModel model = null;
            if (!(String.IsNullOrEmpty(id) || String.IsNullOrWhiteSpace(id)))
            {
                _sqlBO = new NhomDonViBO();
                model = new NhomDonViModel(_sqlBO.GetById(Convert.ToInt32(id)));
            }

            if (model == null)
            {
                model = new NhomDonViModel();
            }
            else
            {
                if (model.IS_CO_BAN == 1) model.BOOL_CO_BAN = true;
                else if (model.IS_CO_BAN == 0) model.BOOL_CO_BAN = false;
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView(model);
            }
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionSave(NhomDonViModel model)
        {
            if (Request.IsAjaxRequest())
            {
                if (model.BOOL_CO_BAN) model.IS_CO_BAN = 1;
                else if (!model.BOOL_CO_BAN) model.IS_CO_BAN = 0;

                _sqlBO = new NhomDonViBO();
                bool result = _sqlBO.Save(model.ToDALModel());
                if (result)
                {
                    if (model.NhomDonViId > 0)
                    {
                        return Json(JsonResponse.Json200("Cập nhật thành công!"));
                    }
                    else
                    {
                        return Json(JsonResponse.Json200("Thêm mới thành công!"));
                    }
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý!"));
                }
            }
            return null;
        }

        
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionDelete(int id)
        {
            if (Request.IsAjaxRequest())
            {
                _sqlBO = new NhomDonViBO();
                bool result = _sqlBO.Delete(id);
                if (result)
                {
                    return Json(JsonResponse.Json200("Xóa thành công"));
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý!"));
                }
            }
            return null;
        }


        [HttpPost]
        public ActionResult ActionSearch(NhomDonViModel model = null, int pageIndex = 0, string sort = "CertificationId", string sortDir = "ASC", string currentId = "")
        {
            var dataPaging = Search(model, pageIndex, currentId, false);
            return PartialView("~/Views/NhomDonVi/_NhomDonViList.cshtml", dataPaging);
        }

        private NhomDonViBO _sqlBO;
        private DataPage<NhomDonViModel> Search(NhomDonViModel myModel, int pageIndex, string currentId, bool compareEqual)
        {
            _sqlBO = new NhomDonViBO();

            var DALModel = _sqlBO.GetListPaging(myModel.ToDALModel(), pageIndex, PageSize);
            var models = new DataPage<NhomDonViModel>();
            var lstItem = DALModel.Data;
            foreach (DHQGHN.DAL.Models.DHQG.NhomDonVi nhomDonVi in lstItem)
            {
                models.Data.Add(new NhomDonViModel(nhomDonVi));
            }

            NhomDonViModel model = null;
            if (!String.IsNullOrEmpty(currentId) && models.Data.Count > 0)
            {
                model = models.Data.Find(user => user.NhomDonViId.Equals(currentId));
                models.CustomData = model;
            }
            else
            {
                models.CustomData = myModel;
            }
            if (models.Data.Count > 0)
            {
                //Tao component phan trang
                var pagNative = new Pagination
                {
                    ActionName = "ActionSearch",
                    ModelName = "NhomDonVi",
                    CurrentPage = pageIndex,
                    InputSearchId = "",
                    TagetReLoadId = "certification-list",

                };
                pagNative.TotalRows = models.Data[0].RecordCount;
                pagNative.CurrentRow = models.Data.Count;
                ViewBag.Pagination = pagNative;
            }
            return models;
        }
	}
}